//
//  bookedViewController.m
//  EPGChina
//
//  Created by CreeveLiu on 15/1/16.
//  Copyright (c) 2015年 sumavision. All rights reserved.
//

#import "BookedViewController.h"

@interface BookedViewController ()

@end

@implementation BookedViewController
{
    NSArray *_arrBookedNotis;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self reloadTableViewContent];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupUI
{
    self.navigationItem.title = NSLocalizedString(@"已预订节目", nil);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"返回", nil)
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(didClickedCancelButton)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"删除全部", nil)
                                                                              style:UIBarButtonItemStylePlain
                                                                             target:self
                                                                             action:@selector(didClickedDeleteAllButton)];
}

- (void)didClickedCancelButton
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didClickedDeleteAllButton
{
    if (_arrBookedNotis.count != 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"确定", nil)
                                                            message:NSLocalizedString(@"删除全部订阅", nil)
                                                           delegate:(id<UIAlertViewDelegate>)self
                                                  cancelButtonTitle:NSLocalizedString(@"取消", nil) otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alertView show];
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"已删除", nil)];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
}

- (void)reloadTableViewContent
{
    _arrBookedNotis = [[UIApplication sharedApplication] scheduledLocalNotifications];
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrBookedNotis.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    NSNotification *noti = [_arrBookedNotis objectAtIndexedSubscript:indexPath.row];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    [cell.textLabel setText:[noti.userInfo objectForKey:@"programName"]];
    [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ %@", [noti.userInfo objectForKey:@"showTime"], [noti.userInfo objectForKey:@"channelName"]]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [[UIApplication sharedApplication] cancelLocalNotification:[_arrBookedNotis objectAtIndex:indexPath.row]];
        [self reloadTableViewContent];
    }
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
